package agriculture.controller;

import agriculture.bean.Comment;
import agriculture.bean.Product;
import agriculture.feign.UserClient;
import agriculture.service.ProductService;
import agriculture.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 查询所有商品，以及根据前端传入的参数进行分类查询
     */
    @PostMapping("/goodsList")
    public Result<List<Product>> getGoodsList(@RequestParam(value = "category", required = false) Long categoryId) {
        List<Product> productList;

        if (categoryId == null) {
            productList = productService.getActiveProducts();
        } else {
            productList = productService.getProductsByCategory(categoryId);
        }

        return new Result<>(200, "查询成功", productList);
    }

    /**
     * 根据ID查询商品的详细信息
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public Result<List<Product>> getProductDetailsByProductId(@PathVariable() Long id) {
        List<Product> products = productService.selectProductDetail(id);
        if (products == null || products.isEmpty()) {
            return new Result<>(404,"查询商品详细失败",null);
        }
        return new Result<>(200,"查询商品详情成功",products);
    }

    /**
     * 根据商品Id获取对应商品的所有评论数据
     * @param productId
     * @return
     */
    @GetMapping("/{productId}")
    public Result<List<Comment>> getCommentsByProductId(@PathVariable Long productId) {
        System.out.println("商品的id：=" +productId);
        List<Comment> comments = productService.getCommentsByProductId(productId);
        System.out.println("最终的评论信息结果："+comments.toString()+"");
        if (comments == null || comments.isEmpty()) {
            return new Result<>(500,"查询评论数据为空",null);
        }
        return new Result<>(200,"获取评论成功", comments);
    }
}
